跳到主要内容

PHP操作mysql服务器

php作为mysql服务器的客户端!

连接认证

发送sql

执行sql,生成结果(mysql-server)

处理结果

关闭连接

加载必要的扩展,使可以作为客户端使用
------------------

mysql\_connect();

![](9D365CE5A5864C2E7ED954805337872F.png)

![](6D4EDDB85F096872557F695E7BDAABD1.png)

连接认证
----

mysql\_connect()

![](7D52E5C3D3F2BE0BFE3145CD1774F749.png)

成功返回连接资源,失败 false!

向mysql服务器发送sql
--------------

mysql\_query(sql, 连接资源);

失败返回false,成功返回资源或者true!

可以使用 mysql \_error(连接) mysql\_errno(连接)获得错误信息和标识

![](C075F6B30320AFCA17B0E3F0CC6752CB.png)

处理返回数据
------

执行成功后:返回数据可以是资源也可以true。执行失败一定是false!

依据所执行的 sql,是否有返回数据!

返回资源:有返回数据:select,show,desc。

返回true:没有返回数据的: use,set,insert,update,delete,DDL

### 处理返回资源

![](BCBA671ED4A55A7176496284D2523552.png)

称之为结果集(result set)类型资源!

结果集:结果的集合!

将数据,从结果集中取出来!称之为 fetch!

使用函数:

mysql\_fetch\_assoc|row|array。功能完全一致,只是返回的数据格式不同!

在结果集中,取得一条记录。结果集内也存在结果集记录指针的概念!

fetch一次,只能取得当前记录,但是可以向后移动记录指针!配合上循环结构可以将所有的记录从结果集中取出!

![](55721D3ECB106594BB474B460D0E25FF.png)

![](B970FB11FB4C4F6550D5C133A9AAF3F7.png)

特别注意:

任何有结果的sql操作,返回的都是结果集!

结果集,就是一个二维表的结构!是一行行的记录组成!

即使,结果集中只有一条记录

甚至,我们只需要返回一条数据!

![](A45B6B66F389A4C58FA97C81FFF5F7EF.png)

释放资源
----

mysql\_free\_result(结果集)

mysql\_close(连接资源);

![](11D1818B4600683CDB214BE82FC9FB3B.png)

预习
==

校对集

列类型(数据类型)

列选项(列属性,列约束)

设计模式(范式,关联)

作业
==

1,模拟查找所有的数据表结构

2,编码问题!

连接的必要四个参数:

![](A06628CFAE50C11418292E997C247B18.png)

客户端发送一条sql:

客户端编码(character\_set\_client)-\>连接层编码(characger\_set\_connection)-\>服务器内部编码(server\_internal)

服务器端发送结果:

服务器内部编码(server\_internal)-\>连接层编码(characger\_set\_connection)-\>客户端接收的结果编码(character\_set\_results)

总体的编码问题:

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c44f)

先获得所有的数据库:

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c453)

为其增加链接,请求到table.php展示所有的表,应该以 GET方式(在url上传递)形式将库名,传递到table.php

![](39C7E8CB9FF37A247E0A2146DC7855AC.png)

table.php

先获得 表名列表,再为其增加指向结构和数据的连接!

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c45a)

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c458)

注意,在获得表结构与数据时,至少需要库名和表名两个参数!

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c457)

column.php获得结构

![](https://leanote.com/api/file/getImage?fileId=58557ce2ab64416d7600c451)

展示:

![](10D440BD57AAEF12636EA42694D12E01.png)

rows.php

展示列表时,先展示字段名,再展示数据!

mysql\_num\_rows($result)获得结果集中的记录数量

=

代码

database.php

1. `\<?php`
2. `header('Content-Type: text/html; charset=utf-8');`
3. `$host = '127.0.0.1';`
4. `$port = '3306';`
5. `$user = 'root';`
6. `$pass = '1234abcd';`
7. `$charset = 'utf8';`
8. `$link = mysql\_connect("$host:$port", $user, $pass);`
9. `mysql\_query('set names ' . $charset, $link);`
10.
11. `//展示所有的数据库`
12. `$sql = "show databases";`
13. `if(!$result = mysql\_query($sql, $link)) {`
14. `echo 'sql执行失败:', $sql , '\<br\>';`
15. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
16. `echo '错误信息:', mysql\_error($link), '\<br\>';`
17. `die;`
18. `}`
19.
20. `echo '\<table\>';`
21. `//遍历结果集,得到所有的记录`
22. `while($row = mysql\_fetch\_assoc($result)) {`
23. `echo '\<tr\>';`
24. `echo '\<td\>';`
25. `echo '\<a href="table.php?dbname=' . $row['Database'] . '"\>';`
26. `echo $row['Database'];`
27. `echo '\</a\>';`
28. `echo '\</td\>';`
29. `echo '\</tr\>';`
30. `}`
31. `echo '\</table\>';`

table.php

1. `\<?php`
2. `header('Content-Type: text/html; charset=utf-8');`
3. `$host = '127.0.0.1';`
4. `$port = '3306';`
5. `$user = 'root';`
6. `$pass = '1234abcd';`
7. `$charset = 'utf8';`
8. `$link = mysql\_connect("$host:$port", $user, $pass);`
9. `mysql\_query('set names ' . $charset, $link);`
10.
11.
12. `//选择默认数据库`
13. `$dbname = $\_GET['dbname'];`
14. `$sql = "use `$dbname`";`
15. `if(!$result = mysql\_query($sql, $link)) {`
16. `echo 'sql执行失败:', $sql , '\<br\>';`
17. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
18. `echo '错误信息:', mysql\_error($link), '\<br\>';`
19. `die;`
20. `}`
21.
22. `//展示所有的表`
23. `$sql = 'show tables';`
24. `if(!$result = mysql\_query($sql, $link)) {`
25. `echo 'sql执行失败:', $sql , '\<br\>';`
26. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
27. `echo '错误信息:', mysql\_error($link), '\<br\>';`
28. `die;`
29. `}`
30. `echo '\<table\>';`
31. `//遍历结果集,取得所有的表`
32. `while($row = mysql\_fetch\_assoc($result)) {`
33. `echo '\<tr\>';`
34. `echo '\<td\>';`
35. `echo $row['Tables\_in\_'.$dbname];`
36. `echo '\</td\>';`
37. `echo '\<td\>';`
38. `echo '\<a href="column.php?tablename=' . $row['Tables\_in\_'.$dbname] . '&dbname=' . $dbname . '"\>';`
39. `echo '结构';`
40. `echo '\</a\>';`
41. `echo '\</td\>';`
42.
43. `echo '\<td\>';`
44. `echo '\<a href="rows.php?tablename=' . $row['Tables\_in\_'.$dbname] . '&dbname=' . $dbname . '"\>';`
45. `echo '数据';`
46. `echo '\</a\>';`
47. `echo '\</td\>';`
48.
49. `echo '\</tr\>';`
50. `}`
51. `echo '\</table\>';`

column.php

1. `\<?php`
2. `header('Content-Type: text/html; charset=utf-8');`
3. `$host = '127.0.0.1';`
4. `$port = '3306';`
5. `$user = 'root';`
6. `$pass = '1234abcd';`
7. `$charset = 'utf8';`
8. `$link = mysql\_connect("$host:$port", $user, $pass);`
9. `mysql\_query('set names ' . $charset, $link);`
10.
11. `//获得结构`
12. `$dbname = $\_GET['dbname'];`
13. `$tablename = $\_GET['tablename'];`
14. `$sql = "desc `$dbname`.`$tablename`"; //desc `itcast`.`stu``
15. `if(!$result = mysql\_query($sql, $link)) {`
16. `echo 'sql执行失败:', $sql , '\<br\>';`
17. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
18. `echo '错误信息:', mysql\_error($link), '\<br\>';`
19. `die;`
20. `}`
21.
22. `//展示,遍历`
23. `echo '\<table\>';`
24. `//遍历结果集,得到所有的记录`
25. `while($row = mysql\_fetch\_assoc($result)) {`
26. `echo '\<tr\>';`
27. `echo '\<td\>';`
28. `echo $row['Field'];`
29. `echo '\</td\>';`
30. `echo '\<td\>';`
31. `echo $row['Type'];`
32. `echo '\</td\>';`
33. `echo '\</tr\>';`
34. `}`
35. `echo '\</table\>';`

rows.php

1. `\<?php`
2. `header('Content-Type: text/html; charset=utf-8');`
3. `$host = '127.0.0.1';`
4. `$port = '3306';`
5. `$user = 'root';`
6. `$pass = '1234abcd';`
7. `$charset = 'utf8';`
8. `$link = mysql\_connect("$host:$port", $user, $pass);`
9. `mysql\_query('set names ' . $charset, $link);`
10.
11. `//选择默认数据库`
12. `$dbname = $\_GET['dbname'];`
13. `$sql = "use `$dbname`";`
14. `if(!$result = mysql\_query($sql, $link)) {`
15. `echo 'sql执行失败:', $sql , '\<br\>';`
16. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
17. `echo '错误信息:', mysql\_error($link), '\<br\>';`
18. `die;`
19. `}`
20.
21. `//获得字段信息`
22. `$tablename = $\_GET['tablename'];`
23. `$sql = "desc `$tablename`";`
24. `if(!$result = mysql\_query($sql, $link)) {`
25. `echo 'sql执行失败:', $sql , '\<br\>';`
26. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
27. `echo '错误信息:', mysql\_error($link), '\<br\>';`
28. `die;`
29. `}`
30. `//展示字段`
31. `echo '\<table border="1"\>';`
32. `//遍历展示结构`
33. `echo '\<tr\>';`
34. `while($row = mysql\_fetch\_assoc($result)) {`
35. `echo '\<th\>';`
36. `echo $row['Field'];`
37. `echo '\</th\>';`
38. `}`
39. `echo '\</tr\>';`
40.
41. `//查询到每个表的数据`
42. `$sql = "select \* from `$tablename` where 1";`
43. `if(!$result = mysql\_query($sql, $link)) {`
44. `echo 'sql执行失败:', $sql , '\<br\>';`
45. `echo '错误代码:', mysql\_errno($link), '\<br\>';`
46. `echo '错误信息:', mysql\_error($link), '\<br\>';`
47. `die;`
48. `}`
49. `//遍历结果集,得到所有的记录`
50. `while($row = mysql\_fetch\_assoc($result)) {`
51. `echo '\<tr\>';`
52. `//得到所有的字段`
53. `foreach($row as $field =\> $value) {`
54. `echo '\<td\>';`
55. `echo $value===''?' ':($value===NULL?'NULL':$value);`
56. `echo '\</td\>';`
57. `}`
58. `echo '\</tr\>';`
59. `}`
60.
61. `echo '\</table\>';`